<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>demo4</title>
  </head>
  <body></body>
  <script src="../dist/axios.js"></script>
  <script>
    const source = axios.CancelToken.source();

    axios.interceptors.request.use(
      config => {
        source.cancel('在发出请求之前的时候取消');
        return config;
      },
      error => {
        console.log(error);
        return Promise.reject();
      }
    );

    axios.interceptors.response.use(
      response => {
        if (response.status === 200) {
          return response.data;
        } else {
          Promise.reject();
        }
      },
      error => {
        return Promise.reject(error);
      }
    );

    axios
      .get('/user?ID=12345', {
        cancelToken: source.token
      })
      .catch(function(e) {
        console.log('Request canceled', e);
      });
  </script>
</html>
